package edu.xjtlu.mobilewebservices.map;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

public class MyOverlays extends ItemizedOverlay<OverlayItem>{
	
	private List<OverlayItem> locations = new ArrayList<OverlayItem>();
	private Drawable marker;
	private Context context;
	final CharSequence[] store_name = {"Name","Address","Tel"};
	
	public MyOverlays(Context context,Drawable defaultMarker) {
		super(defaultMarker);
		this.context = context;
		this.marker = defaultMarker;
		//create Geo point instances
			GeoPoint GP1 = new GeoPoint((int)(ConstData.storeCode[0][0] *1E6),(int)(ConstData.storeCode[0][1] *1E6));
			OverlayItem olt1 = new OverlayItem(GP1,"NJ","ML");

			GeoPoint GP2 = new GeoPoint((int)(ConstData.storeCode[1][0] *1E6),(int)(ConstData.storeCode[1][1] *1E6));
			OverlayItem olt2 = new OverlayItem(GP2,"NJ","ML");
			
			GeoPoint GP3 = new GeoPoint((int)(ConstData.storeCode[2][0] *1E6),(int)(ConstData.storeCode[2][1] *1E6));
			OverlayItem olt3 = new OverlayItem(GP3,"NJ","ML");
			
			GeoPoint GP4 = new GeoPoint((int)(ConstData.storeCode[3][0] *1E6),(int)(ConstData.storeCode[3][1] *1E6));
			OverlayItem olt4 = new OverlayItem(GP4,"NJ","ML");
			
			GeoPoint GP5 = new GeoPoint((int)(ConstData.storeCode[4][0] *1E6),(int)(ConstData.storeCode[4][1] *1E6));
			OverlayItem olt5 = new OverlayItem(GP5,"NJ","ML");

			locations.add(olt1);
			locations.add(olt2);
			locations.add(olt3);
			locations.add(olt4);
			locations.add(olt5);

		populate();
	}

	@Override
	protected OverlayItem createItem(int i) {
		return locations.get(i);
	}

	@Override
	public int size() {
		return locations.size();
	}
	
	//draw map view
	@Override  
    public void draw(Canvas canvas, MapView mapView, boolean shadow) {  
        super.draw(canvas, mapView, shadow);  
        Paint paint = new Paint();
		paint.setStrokeWidth(1);
		paint.setARGB(255, 255, 0, 0);
		paint.setStyle(Paint.Style.STROKE);
		
		//anti alias
		paint.setFlags(Paint.ANTI_ALIAS_FLAG);
		paint.setTextSize(16);
        boundCenterBottom(marker);  
    }  
	
	//instance for user click makers
	protected boolean onTap(int index){
		final int i = index;
        @SuppressWarnings("unused")
		OnClickListener listener = new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				switch(i){
				case 0:
					Toast.makeText(context,"0 case 0", Toast.LENGTH_SHORT).show(); 
					break;
				case 1:
					Toast.makeText(context,"0 case 1", Toast.LENGTH_SHORT).show(); 
					break;
				case 2:
					Toast.makeText(context,"0 case 2", Toast.LENGTH_SHORT).show();
					break;
				case 3:
					Toast.makeText(context,"0 case 3", Toast.LENGTH_SHORT).show();
					break;
				case 4:
					Toast.makeText(context,"0 case 4", Toast.LENGTH_SHORT).show();
					break;
				}
			}
        	
        };
        
      //Display alert dialog with yes and cancel
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(ConstData.store[index]);
		builder.setMessage("This is "+ConstData.store[index]);
		builder.setCancelable(true);
		builder.setNegativeButton("Cancle", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {

			}
		});
		
		AlertDialog dialog = builder.create();
		dialog.show();
		return true;
	};
}
